package com.xhwl.centiotdata.handler.org;

import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.integrateconfig.PedestrianIntegrationConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author pengjunming
 * @description
 * @date 2022/7/4 8:59
 */
@Slf4j
public class OrgFactory {

    private static Map<Integer, OrgHandler> handlerMap = new HashMap<>();

    public static OrgHandler getInvokeStrategy(Integer id) {
        return handlerMap.get(id);
    }

    public static List<Map<PedestrianIntegrationConfigDTO, OrgHandler>> getInvokeStrategyByEnterpriseId(Integer enterpriseId) {
        List<PedestrianIntegrationConfigDTO> enterpriseConfig = PedestrianIntegrationConfig.getEnterpriseConfig(enterpriseId);
        List<Map<PedestrianIntegrationConfigDTO, OrgHandler>> result = new ArrayList<>();
        enterpriseConfig.forEach(t -> {
            OrgHandler invokeStrategy = OrgFactory.getInvokeStrategy(t.getProductId());
            if (Objects.nonNull(invokeStrategy)) {
                Map<PedestrianIntegrationConfigDTO, OrgHandler> map = new HashMap<>();
                map.put(t, invokeStrategy);
                result.add(map);
            }
        });
        return result;
    }

    public static void register(Integer id, OrgHandler handler) {
        if (StringUtils.isEmpty(id)) {
            return;
        }
        handlerMap.put(id, handler);
    }
}
